﻿using OpenCvSharp;
using System.Diagnostics;
using VisionProcess;

namespace TargetFindTest
{
    internal class Program
    {

        public static void CreateDir(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
        }
        public static void Main(string[] args)
        {
            // 测试的图像目录
            string filePath = @"E:\template\Original";
            // 模板匹配加载模板的目录
            string TemplateOriginalDir = @"E:\Template\Original";
            // Xray01模板目录
            string TemplateDir01 = @"E:\Template\Template01";
            // Xray02模板目录
            string TemplateDir02 = @"E:\Template\Template02";

            // 首先是加载所有的模板目录
            OperateResult loadTemplateRes = ImageProcess.LoadTemplate();
            if (!loadTemplateRes.State)
            {
                Console.WriteLine(loadTemplateRes.Message);
                return;
            }

            string keepDir = filePath;
            // 如果不是目录,就先获取目录
            if (!Directory.Exists(keepDir))
            {
                keepDir = Path.GetDirectoryName(keepDir)!;
            }
            string keepOkPath = Path.Combine(keepDir, "ok");
            string keepResPath = Path.Combine(keepDir, "okRes");
            string keepFailPath = Path.Combine(keepDir, "fail");

            CreateDir(keepOkPath);
            CreateDir(keepResPath);
            CreateDir(keepFailPath);

            List<string> fileNames = new List<string>();
            if (File.Exists(filePath))
            {
                fileNames.Append(Path.GetFileName(filePath));
            }
            else
            {
                fileNames = Directory.GetFiles(keepDir)
                    .Select(x => Path.GetFileName(x))
                    .ToList();
            }


            int index = 1;
            int xrayIndex = 1;
            foreach (string fileName in fileNames)
            {
                Console.WriteLine($"\n -------------- 识别的第{index}张图,图像名称为{fileName} ----------------- \n");
                // 根据图像名称获取是第几个xray
                var res = Path.GetFileNameWithoutExtension(fileName).Split(new string[] { "--" }, StringSplitOptions.RemoveEmptyEntries);
                if (res.Length == 0)
                {
                    Console.WriteLine($"文件名称错误,获取是哪个xray的图像失败,文件名称必须是以--01或者--02结尾!");
                    return;
                }

                if (res[^1].Contains("01"))
                {
                    xrayIndex = 1;
                }
                else if (res[^1].Contains("02"))
                {
                    xrayIndex = 2;
                }

                Stopwatch stopwatch = new Stopwatch();
                string filePathReal = Path.Combine(keepDir, fileName);
                Mat imageSrc = Cv2.ImRead(filePathReal, ImreadModes.Grayscale);
                stopwatch.Start();
                OperateResult<Tuple<double, Mat>> targetRes = ImageProcess.FindTargetInfo(imageSrc, xrayIndex);
                index++;
                if (targetRes.State)
                {
                    stopwatch.Stop();
                    Console.WriteLine($"识别成功,识别结果: {targetRes.Data!.Item1:F2},算法耗时: {stopwatch.Elapsed.TotalSeconds} 秒");
                    Cv2.ImWrite(Path.Combine(keepOkPath, fileName), imageSrc);
                    Cv2.ImWrite(Path.Combine(keepResPath, fileName), targetRes.Data.Item2);
                    File.Delete(filePathReal);
                    continue;
                }
                else
                {
                    stopwatch.Stop();
                    Console.WriteLine($"识别失败,算法耗时:{stopwatch.Elapsed.TotalSeconds} 秒");
                    Cv2.ImWrite(Path.Combine(keepFailPath, fileName), imageSrc);
                    File.Delete(filePathReal);
                    continue;
                }
            }
        }
    }
}